package com.ant.storage.controller;


import com.alibaba.excel.EasyExcel;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.BidInformationDto;
import com.ant.storage.dto.HouseLeaseDto;
import com.ant.storage.excel.BidInformationExecl;
import com.ant.storage.excel.InventoryCensusExecl;
import com.ant.storage.pojo.BidInformation;
import com.ant.storage.pojo.HouseLease;
import com.ant.storage.service.IBidInformationService;
import com.ant.storage.service.IHouseLeaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/bidInformation")
public class BidInformationController {

    @Autowired
    private IBidInformationService informationService;

    @ApiOperation(value = "获取物品库存数据列表")
    @GetMapping("/getBidInformationList")
    public ResultPage getBidInformationList(BidInformationDto dto) {
        return ResultPage.success("操作成功", informationService.queryList(dto));
    }

    @ApiOperation(value = "保存信息")
    @PostMapping("/BidInformationSave")
    public ResultVo BidInformationSave(@RequestBody @Valid BidInformation dto, BindingResult result) {
        return informationService.BidInformationSave(dto, result);
    }

    @ApiOperation(value = "根据城市返回投标信息")
    @GetMapping("/getBidInformationUserList")
    public ResultVo getBidInformationUserList() {
        return ResultVo.success("操作成功", informationService.bidInformationUserList());
    }

    @ApiOperation(value = "id查询详情")
    @GetMapping("/findBidInformationById/{id}")
    public ResultVo findBidInformationById(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功", informationService.findBidInformationById(id));
    }


    @ApiOperation(value = "删除")
    @GetMapping("/detele/{id}")
    public ResultVo detele(@PathVariable("id") Integer id) {
        UpdateWrapper<BidInformation> wrapper = new UpdateWrapper<>();
        wrapper.set("is_delete", 0);
        wrapper.eq("id", id);
        return ResultVo.success("操作成功", informationService.update(wrapper));
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/downLoadExportCensus", method = RequestMethod.GET)
    public void downLoadExportCensus(@RequestParam(value = "copmyId", required = false) Integer copmyId,
                                     @RequestParam(value = "time", required = false) List<String> time,
                                     HttpServletResponse response
    ) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("招投信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<BidInformationExecl> orderList = informationService.exportCensus(copmyId,time);
        //需要合并的列
        int[] mergeColumeIndex = {};
        // 从第二行后开始合并
        int mergeRowIndex = 0;
        EasyExcel.write(response.getOutputStream(), BidInformationExecl.class)
                .sheet("结存报表").doWrite(orderList);
        // .registerWriteHandler(new ExcelMergeUtil(mergeRowIndex, mergeColumeIndex))

    }


}
